package com.alliky.user.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alliky.core.entity.vo.GoodsVO;
import com.alliky.core.net.ImageLoader;
import com.alliky.core.router.RoutePath;
import com.alliky.core.base.Fragment;
import com.alliky.core.log.KLog;
import com.alliky.core.utils.RxClick;
import com.alliky.core.widget.SelectableRoundedImageView;
import com.alliky.core.widget.TitleBar;
import com.alliky.user.R;
import com.alliky.user.R2;
import com.alliky.user.mvp.contract.MyContract;
import com.alliky.user.mvp.presenter.MyPresenter;
import com.alliky.user.ui.activity.PersonCenterBaseActivity;
import com.alliky.user.ui.activity.SettingBaseActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

@Route(path = RoutePath.User.MY_FRAGMENT)
public class MyFragment extends Fragment<MyPresenter> implements MyContract.View {

    @BindView(R2.id.titleBar)
    TitleBar titleBar;
    @BindView(R2.id.iv_header)
    CircleImageView ivHeader;
    @BindView(R2.id.rl_person)
    RelativeLayout rlPerson;
    @BindView(R2.id.ll_order)
    LinearLayout llOrder;
    @BindView(R2.id.sriv_banner)
    SelectableRoundedImageView srivBanner;
    @BindView(R2.id.tv_recommend)
    AppCompatTextView tvRecommend;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    BaseQuickAdapter<GoodsVO, BaseViewHolder> mRecommendAdapter;

    @Override
    protected MyPresenter createPresenter() {
        return new MyPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_my;
    }

    @Override
    public void onInitView(Bundle savedInstanceState, View rootView) {
        initRecyclerView();
    }

    @Override
    public void onInitData() {
        super.onInitData();
        titleBar.setRightIconOnClickListener(v -> {
            startActivity(SettingBaseActivity.class);
        });
        getRecommendGoods(true);
    }

    @Override
    public void onInitEvent() {
        RxClick.setOnClickListener(ivHeader, v -> {
            startActivity(PersonCenterBaseActivity.class);
        });
    }

    private void initRecyclerView() {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mRecommendAdapter = new BaseQuickAdapter<GoodsVO, BaseViewHolder>(R.layout.item_recommend_layout) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, GoodsVO goodsVO) {
                ImageLoader.load( goodsVO.getGoods_thumb(), holder.getView(R.id.iv_image));
                holder.setText(R.id.tv_name, goodsVO.getGoods_name());
                holder.setText(R.id.tv_price, "￥" + goodsVO.getMarket_price());
            }
        });
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(refreshLayout1 -> {
            getRecommendGoods(true);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout1 -> {
            getRecommendGoods(false);
        });
    }


    int pageNum = 1;
    int pageSize = 8;

    private void getRecommendGoods(boolean isRefresh) {
        if (isRefresh) {
            pageNum = 1;
        }
        HashMap params = new HashMap();
        params.put("type", 1);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        mPresenter.getRecommendGoods(params, isRefresh);
    }

    @Override
    public void finishRefresh() {
        refreshLayout.finishRefresh();
    }

    @Override
    public void finishLoadMore() {
        refreshLayout.finishLoadMore();
    }

    @Override
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    @Override
    public void showRecommendGoods(List<GoodsVO> goodsVOS, boolean isRefresh) {
        if (isRefresh) {
            mRecommendAdapter.setNewData(goodsVOS);
        } else {
            mRecommendAdapter.addData(goodsVOS);
        }
    }

    int count = 0;

    @OnClick(R2.id.tv_username)
    public void onViewClicked(View view) {
        if (isFastClick(view.getId())) {
            count++;
            KLog.d("count++", " > " + count);
        }
    }
}
